/**
 * Created by Administrator on 2017/3/5 0005.
 */
var Kaleidoscope={
    create:function (texture,_w,_h) {
        var kale={};
        console.log(texture);
        kale.kaleShader={
        uniforms : {
            time:       { value: 1.0 },
            resolution: { value: new THREE.Vector2() },
            iChannel0:    {type: 't', value:texture },
            angle:{value:0.0}
        },

        vertexShader: [
            /**
             * Example Vertex Shader
             * Sets the position of the vertex by setting gl_Position
             */

            // Set the precision for data types used in this shader
            "precision highp float;",
            "precision highp int;",

            // Default THREE.js uniforms available to both fragment and vertex shader


            // Default uniforms provided by ShaderFrog.
            "uniform float time;",

            // Default attributes provided by THREE.js. Attributes are only available in the
            // vertex shader. You can pass them to the fragment shader using varyings
            "attribute vec2 uv2;",

            // Examples of variables passed from vertex to fragment shader
            "varying vec3 vPosition;",
            "varying vec3 vNormal;",
            "varying vec2 vUv;",
            "varying vec2 vUv2;",

            "void main() {",
                // To pass variables to the fragment shader, you assign them here in the
                // main function. Traditionally you name the varying with vAttributeName
                "vNormal = normal;",
                "vUv = uv;",
                "vUv2 = uv2;",
                "vPosition = position;",

                // This sets the position of the vertex in 3d space. The correct math is
                // provided below to take into account camera and object data.
                "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
        "}"
        ].join( '\n' ),

        fragmentShader: [
            "precision highp float;",
            "precision highp int;",
            "uniform sampler2D iChannel0;",
            "uniform vec2 resolution;",
            "uniform float time;",
            "uniform float angle;",
            "varying vec2 vUv;",
            "const float PI = 3.141592658;",
            "const float TAU = 2.0 * PI;",
            "const float sections = 16.0;",
            "void main()",
            "{",
                "vec2 pos = vec2(vUv.xy - 0.5 * resolution.xy) / resolution.y;",
                "float rad = length(pos);",
                "float angle = atan(pos.y, pos.x)+angle;",
                "float ma = mod(angle, TAU / sections);",
                "ma = abs(ma - PI / sections);",
                "float x = cos(ma) * rad;",
                "float y = sin(ma) * rad;",
                "float time = time / 30.0;",
                "gl_FragColor = texture2D(iChannel0, vec2(x - time, y - time));",
            "}",
            "float Tile1D(float p, float a)",
            "{",
                "p -= 4.0 * a * floor(p / 4.0 * a);",
                "p -= 2. * max(p - 2.0 * a, 0.0);",
                "return p;",
            "}"
            ].join( '\n' )
        }


        // kale.init=function(_w,_h)
        // {
            var geometry = new THREE.PlaneBufferGeometry( _w, _h );
            var material = new THREE.ShaderMaterial( {
                uniforms: kale.kaleShader.uniforms,
                vertexShader: kale.kaleShader.vertexShader,
                fragmentShader: kale.kaleShader.fragmentShader,
                side:THREE.DoubleSide,
                transparent:true
            } );
            console.log(material.uniforms.iChannel0);
            material.uniforms.iChannel0.value.wrapS = material.uniforms.iChannel0.value.wrapT = THREE.RepeatWrapping;
            kale.mesh = new THREE.Mesh( geometry, material );
        // }

        return kale;
    }
}